//
//  NYBaseTopicViewController.m
//  百思不得姐
//
//  Created by Apple on 15/8/23.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYBaseTopicViewController.h"
#import "NYTopicModel.h"
#import "NYAllViewCell.h"
#import <MJRefresh.h>
#import "NYNewViewController.h"
#import "NYConmentViewController.h"

@interface NYBaseTopicViewController ()
/* manager */
@property(nonatomic , strong) AFHTTPSessionManager *manager;

/* all数据 */
@property(nonatomic , strong) NSMutableArray *topics;
/* maxtime */
@property(nonatomic , copy) NSString *maxtime;


- (NSString *)aParma;
@end

@implementation NYBaseTopicViewController
#pragma mark - 懒加载
- (NYTopicCellType)cellType
{
    return NYTopicCellTypeAll;
}
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}
- (NSMutableArray *)topics
{
    if (_topics == nil) {
        _topics = [NSMutableArray array];
    }
    return _topics;
}
static NSString *ID = @"topicCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self setupRefresh];
}
#pragma mark - 刷新控件
- (void)setupRefresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)];
    header.automaticallyChangeAlpha = YES;
    // header.ignoredScrollViewContentInsetTop = NYnavigationBarMaxY;
    self.tableView.header = header;
    [self.tableView.header beginRefreshing];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
    
}
#pragma mark - 初始化tableview
- (void)setupTableView
{
    self.tableView.contentInset = UIEdgeInsetsMake(NYnavigationBarMaxY + 35, 0, 49, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NYAllViewCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
}
- (NSString *)aParma
{
    return [self.parentViewController isKindOfClass:[NYNewViewController class]]? @"newlist":@"list";
}
#pragma mark - 加载数据
- (void)loadNewTopics
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"]= self.aParma;
    parameters[@"c"] = @"data";
    parameters[@"type"] = @(self.cellType);
    NYWeakSelf;
    [self.manager GET:NYBaseURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        [weakSelf.tableView.header endRefreshing];
        _maxtime = responseObject[@"info"][@"maxtime"];
#warning 获取服务器数据，写到本地plist方便查看
        //[responseObject writeToFile:@"/Users/apple/Desktop/1.plist" atomically:YES];
        weakSelf.topics = [NYTopicModel objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [weakSelf.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        if (error.code == NSURLErrorCancelled) {
            return ;
        }
            [SVProgressHUD showErrorWithStatus:@"网络不接力~"];
    }];
    
}
- (void)loadMoreTopics
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"]= self.aParma;
    parameters[@"c"] = @"data";
    parameters[@"type"] = @(self.cellType);
    parameters[@"maxtime"] = _maxtime;
    NYWeakSelf;
    [self.manager GET:NYBaseURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.maxtime = responseObject[@"info"][@"maxtime"];
        NSArray *newTopics = [NYTopicModel objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [weakSelf.topics addObjectsFromArray:newTopics];
        
        [weakSelf.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [weakSelf.tableView.footer endRefreshing];
        if (error.code == NSURLErrorCancelled) {
            return ;
        }
        [SVProgressHUD showErrorWithStatus:@"网络不接力~"];
        
    }];
    
}
#pragma mark - <tableview数据源方法>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topics.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NYAllViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.topic = self.topics[indexPath.row];
    return cell;
}
#pragma mark - <tableview代理方法>
//计算cell的高度，拿到模型进行设置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NYTopicModel *topic = self.topics[indexPath.row];
    
    return topic.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NYConmentViewController *conment = [[NYConmentViewController alloc]init];
    conment.topic = self.topics[indexPath.row];
    [self.navigationController pushViewController:conment animated:YES];
}
@end
